package com.ruozedata.bigdata.lesson02

import scala.annotation.tailrec

object FunctionApp {

    def main(args: Array[String]): Unit = {
        val x: Int = max(1,2)
        println(x)

        println(add)
    }

    // 最后一行默认作为返回值
    def max(x:Int, y:Int): Int = {
        if (x > y) {
            x
        } else {
            y
        }
    }

    def add(): Int = 1 + 2

    def sum(nums:Int*) = {
        var  res = 0
        for (ele <- nums) {
            res += ele
        }
        res
    }

    println(sum(1, 2, 3, 4))
    println(sum(1 to 100 : _*))  // ? 语法糖

    def printCourses(courses:String*): Unit = {
        courses.foreach(x => println(x))
    }

    val arr: Array[String] = Array("a", "b")
    printCourses(arr : _*)


    // 对于递归 显示写返回值类型
    def test(x:Int): Long = {
        if (x == 1) 1
        else x * test(x - 1)
    }

    // 尾递归
    // 中间使用累加器 解决zhai溢出
    @tailrec
    def test2(x:Int, acc:BigInt): BigInt = {
        if (x==1) acc
        else test2(x-1, x*acc)
    }

    println(test2(100000, 1))
}
